package com.justspeak.util.xml;

import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.justspeak.module.CourseInfo;

/** 
 * copyright    justspeak 2010
 * @date        2011-9-13 V1.0初始作成
 * @author      zhouhongbing
 * @version     V1.0
 * @brief       CourceListContentHandle的定义
 * @since       1.0
 * @history     2011-9-13 V1.X 修改内容，一个版本一行
 * @descript    类的概要说明:处理课程列表的xml文档
 * @todo        未完成的事项
 */
public class CourceListContentHandle extends DefaultHandler
{
    /**
	 * @param courseInfos
	 */
	public CourceListContentHandle(List<CourseInfo> courseInfos) {
		super();
		this.courseInfos = courseInfos;
	}

	/**
	 * 
	 */
	public CourceListContentHandle() {
		super();
	}

	private List<CourseInfo> courseInfos=null;
    
    private CourseInfo courseInfo=null;
    
    private String tagName=null;

    /** 
     * 解析元素节点
     **/
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException
    {
     
        String node=new String(ch,start,length);
        if(tagName.equals("id")){
            courseInfo.setId(node);
        }else if(tagName.equals("book")){
            courseInfo.setBook(node);
        }else if(tagName.equals("unit")){
            courseInfo.setUnit(node);
        }else if(tagName.equals("unitname")){
            courseInfo.setUnitname(node);
        }else if(tagName.equals("mp3.name")){
            courseInfo.setMp3Name(node);
        }else if(tagName.equals("mp3.size")){
            courseInfo.setMp3Size(node);
        }else if(tagName.equals("lrc.name")){
            courseInfo.setLrcName(node);
        }else if(tagName.equals("lrc.size")){
            courseInfo.setLrcSize(node);
        }
    }

    /** 
     * 重写endDocument方法
     **/
    @Override
    public void endDocument() throws SAXException
    {
        super.endDocument();
    }

    /** 
     * 重写endElement方法，元素结束时，讲courseInfo对象添加到列表中
     **/
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException
    {
       if(qName.equals("resource")){
           courseInfos.add(courseInfo);
       }
       tagName="";
    }

    /**
     *重写startDocument
     **/
    @Override
    public void startDocument() throws SAXException
    {
        super.startDocument();
    }

    /** 
     * 重写startElement标签，开始读取courseInfo对象
     **/
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException
    {
      tagName=localName;
      if(tagName.equals("resource")){
          courseInfo=new CourseInfo();
      }
        
    }

    /**
     * @param courseInfos 赋值 courseInfos
     */
    public void setCourseInfos(List<CourseInfo> courseInfos)
    {
        this.courseInfos = courseInfos;
    }

    /**
     * @return courseInfos
     */
    public List<CourseInfo> getCourseInfos()
    {
        return courseInfos;
    }

}
